from socket import *

# 封装Http服务端
ip = "127.0.0.1"
port = 8000

class HttpServer:
    def __init__(self):
        self.isrun = False
        self.listensock = None

    def init(self):
        self.listensock = socket(AF_INET, SOCK_STREAM)
        self.listensock.bind((ip, port))
        self.listensock.listen(5)

    def _EchoTask(self, newsock):
        # 获取客户端请求
        maxlen = 1024
        inbuffer = newsock.recv(maxlen)
        if inbuffer:
            print("-----客户端请求------")
            print(inbuffer.decode())
        else:
            print("客户端关闭了数据...")

        # 自己指定响应报文数据
        header = "HTTP/1.0 200 OK\n"
        kv = "Content - Type: text/html\ncharset=UTF-8\nSet-Cookie: username=12346;expires="'100'"\n"
        empty_line = "\n"
        body = '''<!DOCTYPE html>
                <html lang="zh - CN">
                <head>
                <meta charset="UTF - 8">
                <meta name="viewport" content="width=device - width, initial - scale=1.0">
                <title>百度一下，你就知道</title>
                <style>
                    body {
                        font - family: Arial, sans - serif;
                        text - align: center;
                        margin - top: 2px;
                    }
                    ul.menu {
                        list - style - type: none;
                        margin: 0;
                        padding: 0;
                        overflow: hidden;
                        background - color: white;
                    }
                    ul.menu li {
                        float: left;
                    }
                    ul.menu li a {
                        font - size: 12px;
                        display: block;
                        color: black;
                        text - align: center;
                        padding: 8px 10px;
                        text - decoration: none;
                    }
                    ul.menu li a:hover {
                        color: #4662d9;
                    }
                    .logo {
                        margin - bottom: 20px;
                    }
                    .search - box {
                        width: 500px;
                        height: 35px;
                        border: 2px solid #4e6ef2;
                        padding: 0 10px;
                        font - size: 16px;
                        border - radius: 10px;
                        outline: none;
                    }
                    .search - button {
                        width: 100px;
                        height: 38px;
                        background - color: #4e6ef2;
                        color: white;
                        border: none;
                        font - size: 16px;
                        border - radius: 10px;
                        cursor: pointer;
                        margin - left: 2px;
                    }
                    .search - button:hover {
                        background - color: #4662d9;
                    }
                </style>
                </head>
                <body>
                <ul class="menu">
                    <li><a href="https://news.baidu.com/">新闻</a></li>
                    <li><a href="https://map.baidu.com/">地图</a></li>
                    <li><a href="https://tieba.baidu.com/index.html">贴吧</a></li>
                    <li><a href="https://haokan.baidu.com/?sfrom=baidu - top">视频</a></li>
                    <li><a href="https://image.baidu.com/">图片</a></li>
                    <li><a href="https://pan.baidu.com/">网盘</a></li>
                    <li><a href="https://wenku.baidu.com/">文库</a></li>
                    <li><a href="https://chat.baidu.com/">AI助手</a></li>
                </ul>
                <div class="logo">
                    <img src="https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png" alt="百度logo" width="240" height="120">
                </div>
                <div>
                    <input type="text" class="search - box" placeholder="请输入搜索内容">
                    <button class="search - button">百度一下</button>
                </div>
                </body>
                </html>'''
        response = header + kv + empty_line + body
        # 回一个请求
        newsock.sendall(response.encode())  # 序列化
        newsock.close()

    def Start(self):
        self.isrun = True
        while self.isrun:
            print("正在等待新链接...")
            newsock, addr = self.listensock.accept()
            if newsock:
                print(f"get a link {addr}")
            self._EchoTask(newsock)


svr = HttpServer()
svr.init()
svr.Start()